package com.example.pickerview;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getName();
    TextView textView = null;
    TextView text2 = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //解析数据 （省市区三级联动）
        Utils.initJsonData(this);

        textView = findViewById(R.id.textView);
        textView.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                // 弹出选择器（省市区三级联动）
//                showPickerView();
                //弹出对话框
//                showDialogView();
                Intent intent = new Intent(MainActivity.this, TestActivity.class);
                intent.putExtra("one","1");
                intent.putExtra("two","2");
                startActivity(intent);
            }
        });
    }
    View dialogView = null;
    private void showDialogView(){
        dialogView = getLayoutInflater().inflate(R.layout.dialog_view, null);
        text2 = dialogView.findViewById(R.id.text22);
        text2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPickerView();
            }
        });
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.mipmap.ic_launcher).setTitle("我是对话框")
                .setMessage("今天天气怎么样")
                .setView(dialogView)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.e(TAG,"点击了确定");
//                        showPickerView();
                    }
                })
                .setNegativeButton("取消",new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.e(TAG,"点击了取消");
                    }
                })
                .create()
                .show();
    }

    // 弹出选择器（省市区三级联动）
    private void showPickerView() {

        OptionsPickerView pvOptions = new OptionsPickerBuilder(dialogView.getContext(), new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                JsonBean jsonBean = Utils.options1Items.get(options1);
                //返回的分别是三个级别的选中位置,更改地址信息
                text2.setText(Utils.options1Items.get(options1).getPickerViewText() + "  "
                        + Utils.options2Items.get(options1).get(options2) + "  "
                        + Utils.options3Items.get(options1).get(options2).get(options3));
                Log.i("tag","切换到"+Utils.options3Items.get(options1).get(options2).get(options3)+"的城市");
                //显示揽收的数据
//                showData(Utils.options3Items.get(options1).get(options2).get(options3));
            }
        })
                .setTitleText("城市选择")
                .setDividerColor(Color.BLACK)
                .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                .setContentTextSize(20)
                .build();
//        pvOptions.setPicker(options1Items);//一级选择器
//        pvOptions.setPicker(options1Items, options2Items);//二级选择器
        pvOptions.setPicker(Utils.options1Items, Utils.options2Items, Utils.options3Items);//三级选择器
        pvOptions.show();
    }
}